<?php
$error = '';
if(isset($_POST['submit']))
{	
	if($_POST['page_name'] == '' || $_POST['page_content'] == '')
	{
		$error = 'Please fill in the mandatory fields';
	}
	else
	{
		$current_page_name = preg_replace('/(\s)+/','_',$_POST['page_name']); //replace the spaces in page name with underscore
		$query = " SELECT id FROM pages WHERE name = '".$current_page_name."' AND id != ".$_REQUEST['id'];
		if(mysql_num_rows(mysql_query($query)) > 0 )
		{
			$error = 'Page name is not available, please choose another one';
		}
		else
		{
			//$query = " INSERT INTO pages(name,content,meta_description,meta_keywords,title) VALUES('".$current_page_name."','".$_POST['page_content']."','".$_POST['meta_description']."','".$_POST['meta_keywords']."','".$_POST['page_title']."') ";
			$query = " UPDATE pages SET name = '".$current_page_name."', content = '".$_POST['page_content']."', meta_description = '".$_POST['meta_description']."', meta_keywords = '".$_POST['meta_keywords']."', title = '".$_POST['page_title']."' WHERE id = ".$_REQUEST['id'];
			mysql_query($query);
			save_page($current_page_name, $_POST['page_content']);
			header('location:index.php?page=listing&sm=Page updated successfully'); exit;
		}
	}
}
$query = " SELECT * FROM pages WHERE id = '".$_REQUEST['id']."'";
$row = mysql_fetch_array(mysql_query($query));
$current_page_name = str_replace('_',' ',$row['name']);
?>
<h2>Edit Page</h2>
<?php if(!empty($error)) { ?>
<div class="error"><?php echo $error; ?> </div>
<?php } ?>
<form method="post">
<table cellpadding="2" cellspacing="2" border="0" >
	
	<tr style="background-color:#FFFFFF;">
		<td>Page Name<span style="color:F0F0F0;"> *</span></td><td><input type="text" name="page_name" value="<?php echo $current_page_name; ?>" /></td>
	</tr>
	<tr style="background-color:#FFFFFF;">		
		<td>Page Content<span style="color:F0F0F0;"> *</span></td><td><textarea cols="20"  rows="" name="page_content"><?php echo $row['content']; ?></textarea></td>			
	</tr>
	<tr style="background-color:#FFFFFF;">
		<td>Meta Description</td><td><textarea cols="20" rows="" name="meta_description"><?php echo $row['meta_description']; ?></textarea></td>
	</tr>
	<tr style="background-color:#FFFFFF;">
		<td>Meta Keywords</td><td><textarea cols="20" rows="" name="meta_keywords"><?php echo $row['meta_keywords']; ?></textarea></td>			
	</tr>
	<tr style="background-color:#FFFFFF;">
		<td>Title</td><td><input type="text" name="page_title" value="<?php echo $row['title']; ?>" /></td>			
	</tr>
	<tr style="background-color:#FFFFFF;">
		<td colspan="2"><input type="submit" name="submit" value="Update" /></td>			
	</tr>
	<input type="hidden" name="id" value="<?php echo $_REQUEST['id']; ?>" />
</table>	
</form>	